﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BSE.Windows.Test
{
    public partial class DrugTypeForm : BaseForm
    {
        public DrugTypeForm()
        {
            InitializeComponent();
        }

        public override void RefreshForm()
        {
            FillDrugList();
        }

        private void DrugTypeForm_Load(object sender, EventArgs e)
        {
            FillDrugList();
        }

        private void FillDrugList()
        {
            List<Yaopin> yaopins = AnesthesiaBLL.GetAllYaopin();

            listView1.Items.Clear();
            foreach (Yaopin y in yaopins)
            {
                ListViewItem lvit = new ListViewItem(y.YaopinCode);
                lvit.Tag = y;
                
                ListViewItem.ListViewSubItem lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.YaopinName;
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.YaopinGuige;
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.YaopinDanwei;
                lvit.SubItems.Add(lvsit);

                listView1.Items.Add(lvit);
            }
        }

        private void toolStripAddDrug_Click(object sender, EventArgs e)
        {
            YaopinForm form = new YaopinForm();
            if (DialogResult.OK == form.ShowDialog())
            {
                try
                {
                    AnesthesiaBLL.AddYaopin(form.yaopin);
                    FillDrugList();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("添加药品失败！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                MessageBox.Show("请先选择药品！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }

            if (listView1.SelectedItems.Count > 1)
            {
                MessageBox.Show("一次只能修改一种药品的信息！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }

            Yaopin y = listView1.SelectedItems[0].Tag as Yaopin;
            YaopinForm form = new YaopinForm();
            form.yaopin = y;
            if (DialogResult.OK == form.ShowDialog())
            {
                try
                {
                    AnesthesiaBLL.ModifyYaopin(form.yaopin);
                    FillDrugList();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("修改药品失败！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                MessageBox.Show("请先选择药品！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }

            if (MessageBox.Show("药品删除后将不能再使用。是否确定要删除？", "医院", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                try
                {
                    for (int i = 0; i < listView1.SelectedItems.Count; i++)
                    {
                        Yaopin y = listView1.SelectedItems[i].Tag as Yaopin;
                        AnesthesiaBLL.DeleteYaopin(y);
                    }


                    FillDrugList();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("删除药品失败！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
        }
    }
}
